import axios from 'axios';
import { Local } from '@/utils/storage';

// 配置新建一个 axios 实例
const service = axios.create({
	baseURL: process.env.VITE_APP_BASE_URL as any,
	timeout: 10000,
	headers: {'Accept-Language':Local.get('lang')||'cn'},
});

// 添加请求拦截器
service.interceptors.request.use(
	(config:any) => {
		// 在发送请求之前做些什么 token
		if (Local.get('token')) {
			config.headers.common['token'] = `${Local.get('token')}`;
		}
		return config;
	},
	(error) => {
		// 对请求错误做些什么
		return Promise.reject(error);
	}
);

// 添加响应拦截器
service.interceptors.response.use(
	(response:any) => {
		const res = response.data;
		if(res.status+'' === '200'){
			return res;
		}else{
			return Promise.reject({message:res.data});
		}
	},
	(error) => {
		return Promise.reject(error);
	}
);

// 导出 axios 实例
export default service;